<?php
namespace App\Service;

use Illuminate\Support\Facades\DB;

class ScGoodsService extends BaseService
{
    //商品

    /**
     * 分类列表
     */
    public function getClist()
    {
        $db = $this->dbConn($this->dbScCate)->where('del', 0);
        $res = $db->get();
        if (!count($res)) {
            return array('code' => -2, 'msg' => '没有分类');
        }
        $arys = [];
        foreach ($res as $v) {
            $v = (array) $v;
            $parentOne = (array) $this->dbConn($this->dbScCate)->where('id', $v['pid'])->first();
            $pname = empty($parentOne) ? '顶层分类' : $parentOne['title'];
            $img = $v['img'] ? $this->imgDomain.'/' . $v['img'] : '-';
            $arys[] = [
                'title' => $v['title'],
                'img' => $img,
                'pname' => $pname,
                'uptime' => $v['uptime'],
            ];
        }
        return array('code' => 0, 'msg' => 'success', 'data' => $arys);
    }

    /**
     * 商品列表
     */
    public function getGlist()
    {
        $db = $this->dbConn($this->dbScGoods)->where('del', 0);
        $res = $db->get();
        if (!count($res)) {
            return $this->toback(-2, '没有商品');
        }
        $arys = [];
        foreach ($res as $v ) {
            $v = (array) $v;
            $cateOne = (array) $this->dbConn($this->dbScCate)
                ->where('del', 0)
                ->where('id', $v['cate_id'])
                ->first();
            $cate_name = !empty($cateOne) ? $cateOne['title'] : '';
            $arys[] = [
                'id' => $v['id'],
                'gno' => $v['gno'],
                'title' => $v['title'],
                'img' => $this->imgDomain . '/' . $v['bigimg'],
                'cate_name' => $cate_name,
                'sale' => $v['sale'],
                'isnew' => $v['isnew'],
            ];
        }
        return $this->toback(0, '', $arys);
    }

    /**
     * 订单列表
     * @param array $pas
     *      【status：1未付款，2审核中，3未发货；keyword：搜索词，phone：手机号】
     */
    public function getOlist($pas)
    {
        $db = $this->dbConn($this->dbScOrder)->where('del', 0);
        $status = $pas['status'];
        $kword = $pas['keyword'];
        $phone = $pas['phone'];
        $ws = [];
        if ($status == 1) {
            $ws[] = ['iscancel', '=', 1];
            $ws[] = ['status', '=', 1];
            $db = $db->where($ws);
        } else if ($status == 2) {
            $ws[] = ['iscancel', '=', 2];
            $db = $db->where($ws);
        } else if ($status == 3) {
            $ws[] = ['iscancel', '=', 1];
            $ws[] = ['delivery', '=', 1];
            $ws[] = ['status', '=', 2];
            $db = $db->where($ws);
        } else {
        }
        if ($kword) {
            $db = $db->where('orderno', 'like', '%'.$kword.'%');
        }
        if ($phone) {
            $uOnes = $this->dbConn($this->dbUser)->where('phone', 'like', '%'.$phone.'%')->get();
            if (count($uOnes)) {
                $uids = [];
                foreach ($uOnes as $v) {
                    $v = (array) $v;
                    $uids[] = $v['id'];
                }
                $db = $db->whereIn('uid', $uids);
            }
        }
        $total = $db->count();
        $res = $db->get();
        if (!count($res)) {
            return $this->toback(-2, '没有订单');
        }
        $status1s = $this->orderStatus1s;
        $status2s = $this->orderStatus2s;
        $cancels = $this->orderCancels;
        $arys = [];
        foreach ($res as $v) {
            $v = (array) $v;
            //SKU信息
            $skuOnes = $this->dbConn($this->dbScOsku)->where('order_id', $v['id'])->get();
            $sku_name = '-';
            $priceTotal = $v['price'];
            $gname = '-';
            $gimg = '';
            $num = 0;
            if (count($skuOnes)) {
                $skuOne = (array) $skuOnes[0];
                $sku_name = $skuOne['sku_name'];
                $gname = $skuOne['gname'];
                foreach ($skuOnes as $skuOne) {
                    $skuOne = (array) $skuOne;
                    $num += $skuOne['num'];
                }
            }
            $v['price'] = $priceTotal / 100;
            $v['real_pay'] = $v['real_pay'] / 100;
            $v['cancel_name'] = array_key_exists($v['iscancel'], $cancels) ? $cancels[$v['iscancel']] : '-';
            if ($v['iscancel'] > 1) {
                $v['status_name'] = array_key_exists($v['iscancel'], $cancels) ? $cancels[$v['iscancel']] : '';
            } else if ($v['delivery'] == 1) {
                $v['status_name'] = array_key_exists($v['status'], $status1s) ? $status1s[$v['status']] : '';
            } else {
                $v['status_name'] = array_key_exists($v['status'], $status2s) ? $status2s[$v['status']] : '';
            }
            $v['sku_name'] = $sku_name;
            $v['gname'] = $gname;
            $v['gimg'] = $gimg;
            $v['num'] = $num;
            //收件人信息
            $v['accepter'] = ''; $v['acceptDoor'] = '';
            $addressOne = (array) $this->dbConn($this->dbUaddress)->where('id', $v['address_id'])->first();
            $v['accepter'] = empty($addressOne) ? '' : $addressOne['accepter'].'-'.$addressOne['phone'];
            $v['acceptDoor'] = empty($addressOne) ? '' : $addressOne['wx_name'].'-'.$addressOne['door'];
            $uOne = (array) $this->dbConn($this->dbUser)->where('id', $v['uid'])->first();
            $v['phone'] = empty($uOne) ? '-' : $uOne['phone'];
            $arys[] = $v;
        }
        return $this->toback(0, '', $arys, $total);
    }
}